package myzf.cloud;



import cn.myzf.module.container.WebConfigContainer;
import cn.myzf.module.controller.AppController;
import cn.myzf.module.instance.ControllerInstance;
import cn.myzf.module.instance.InstanceConfig;
import cn.myzf.module.register.Register;
import cn.myzf.module.routes.HttpServlet;
import cn.myzf.module.routes.NettyDispatchcher;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(ServerContext.class) //做容器初始化 以及服务剔除功能实现(定时器)
@ConditionalOnBean(NettyServerAutoConfigMarker.class)
@EnableConfigurationProperties(RegisterConfigProperties.class)
public class AppConfig
{

    @Bean
    public AppController appController(Register register, InstanceConfig instanceConfig){
        AppController appController = new AppController(register, instanceConfig);
        return appController;
    }

    @Bean
    public InstanceConfig instanceConfig(){
        return  new InstanceConfigImpl();
    }

    @Bean
    public ControllerInstance controllerInstance(AppController appController){
        ControllerInstance controllerInstance = new ControllerInstanceImpl();
        controllerInstance.getResourceInstances().put(AppController.class,appController);
        return controllerInstance;
    }

    @Bean
    public Register register(){
        EventInstanceRegister register = new EventInstanceRegister();
        return  register;

    }


    @Bean(initMethod = "start",destroyMethod = "stop")
    public WebConfigContainer container(NettyDispatchcher nettyDispatchcher) {
        WebConfigContainer webConfigContainer = new WebConfigContainer();
        webConfigContainer.setHttpServlet(nettyDispatchcher);
        return webConfigContainer;
    }

    @Bean
    public NettyDispatchcher nettyDispatchcher(ControllerInstance controllerInstance){
        NettyDispatchcher nettyDispatchcher = new NettyDispatchcher();
        nettyDispatchcher.setControllerInstance(controllerInstance);
        return nettyDispatchcher;
    }

}
